export function getBirthTime() {
	return new Promise((resolve, reject) => {
		uni.getStorage({
			key: 'LIFE_BIRTCH_DATE',
			success: function(res) {
				resolve(res.data)
			},
			fail() {
				reject('fail')
			}
		})
	})
}

export function setBirthTime(value) {
	return new Promise((resolve, reject) => {
		uni.setStorage({
			key: 'LIFE_BIRTCH_DATE',
			data: value,
			complete() {
				resolve()
			}
		})
	})
}

export function getDeadTime() {
	return new Promise((resolve, reject) => {
		uni.getStorage({
			key: 'LIFE_DEAD_DATE',
			success: function(res) {
				resolve(res.data)
			},
			fail() {
				reject('fail2')
			}
		})
	})
}

export function setDeadTime(value) {
	return new Promise((resolve, reject) => {
		uni.setStorage({
			key: 'LIFE_DEAD_DATE',
			data: value,
			complete() {
				resolve()
			}
		})
	})
}

// 计算生存时间
export function getLifeTime(nowDate = new Date, birthDate, isAll = false) {
	const res = {};
	if(!birthDate) {
		return res;
	}
	let birthYear = birthDate.getFullYear();
	let nowYear = nowDate.getFullYear();

	let birthMonth = birthDate.getMonth();
	let nowMonth = nowDate.getMonth();

	let birthDay = birthDate.getDate();
	let nowDay = nowDate.getDate();

	// 计算生存时间 生存年 lifeYear
	let lifeYear = nowYear - birthYear;

	if (isAll) {
		// 生存月 lifeMonth
		let lifeMonth = lifeYear * 12 + nowMonth - birthMonth;
		// 生存毫秒
		const lifeMS = nowDate - birthDate;
		// 生存分
		const lifeMinute = lifeMS / 1000 / 60;
		// 生存小时
		const lifeHours = lifeMinute / 60;
		// 生存小时
		const lifeDay = lifeHours / 24;
		// 生存周
		const lifeWeak = lifeDay / 7;

		res.lifeMonth = Math.floor(lifeMonth);
		res.lifeWeak = Math.floor(lifeWeak);
		res.lifeDay = Math.floor(lifeDay);
		res.lifeHours = Math.floor(lifeHours);
		res.lifeMinute = Math.floor(lifeMinute);
	}

	let nextBirth = new Date(nowYear, birthMonth, birthDay);
	if (nextBirth - nowDate < 0) { // 判断是否过了今年的生日
		nextBirth = new Date(nowYear + 1, birthMonth, birthDay);
	} else {
		lifeYear -= 1;
	}
	let yearTime = nextBirth - new Date(nextBirth.getFullYear() - 1, birthMonth, birthDay);
	let temp = nextBirth - nowDate;
	// 生存时间
	const lifeTime = lifeYear + (yearTime - temp) / yearTime;
	res.lifeTime = lifeTime;
	res.lifeYear = lifeYear;
	return res;
}
